#!/bin/bash
# -*- coding: utf-8 -*-
#
#  Uruk Update Manager
#  
#  Copyright 2022 hayder majid <hayder@riseup.net>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.



#result=$(ps aux | grep -i "/usr/bin/uruk-refresh-cache" | grep -v "grep" | wc -l)
#if [ $result -ge 1 ]
   #then
        #killall /usr/bin/uruk-refresh-cache
   #else
        #/usr/bin/uruk-refresh-cache  Err:
#fi
NUM=0
NUMUPGRADE=${NUM}
UPGRADEMODE="--upgrade-mode"
#MESS1=$(echo -e "Your System is up to date!"; echo)
TITL=$(echo -e "System Updating Info"; echo);
TITL1=$(echo -e "Checking for updates"; echo);
TEXT1=$(echo -e "Checking your System for updates \n this may take a few minutes depending on your speed connection"; echo);
UPGM=$(echo -e "Upgrade System"; echo);
UPGL=$(echo -e "View packages"; echo);
OKL=$(echo -e "Ok"; echo);
CANL=$(echo -e "Cancel"; echo)
ERR=$(echo -e "Opps! Something went wrong\!, \n please try again later"; echo)
MESS2=$(echo -e "You have  $NUMUPGRADE  packages needed to be upgrade\! \n \n if you want to continue press \" $UPGM \" \n to continue, or \" $UPGL \" to show upgraded list packages , or \" $CANL \" to exit this window"; echo)

UPG(){
    ANS=$(zenity --info --title="$TITL" --text="$MESS2" --ok-label="$CANL" --extra-button="$UPGM" --extra-button="$UPGL" --width=400 --height=200  --window-icon=/usr/share/pixmaps/urukupdater.svg)
	RES=$?  
	VAR="${RES}-${ANS}"
	if [ "$VAR" = "0-" ]; then 
	    exit 0;
	elif [ "$VAR" = "1-$UPGM" ]; then 
	    pkexec /usr/sbin/synaptic --hide-main-window $UPGRADEMODE --non-interactive --parent-window-id %d;
       else
            zenity --text-info   --title="Updateing list" --ok-label="$UPGM" --filename=/usr/share/urukUpdater/status/upd.list --window-icon=/usr/share/pixmaps/urukupdater.svg;
            rc=$?
            case $rc in
                0)
                  pkexec /usr/sbin/synaptic --hide-main-window $UPGRADEMODE --non-interactive --parent-window-id %d;
                ;;
                1)
                  exit 0
                ;;
                -1)
                  zenity --error --text="$ERR " --width=200 --height=130  --window-icon=/usr/share/pixmaps/urukupdater.svg
                ;;
            esac
	fi
}

UPD(){
if [ "$NUMUPGRADE" != 0 ] ; then
  UPG
  sudo /usr/lib/urukUpdater/checker
  sudo /usr/lib/urukUpdater/setter
  sudo /usr/lib/urukUpdater/restart
elif [ "$NUMUPGRADE" = 0 ] ; then
  sudo /usr/lib/urukUpdater/checker
  sudo /usr/lib/urukUpdater/setter
  sudo /usr/lib/urukUpdater/restart
fi
}
if grep -q "Err:" "/usr/share/urukUpdater/status/err" || grep -q "E:" "/usr/share/urukUpdater/status/err" || grep -q "Err:" "/usr/share/urukUpdater/status/result"; then
  zenity --error --text="$ERR " --width=200 --height=130  --window-icon=/usr/share/pixmaps/urukupdater.svg
else
  UPD
fi
exit 0
